package com.example.sell.controller;

import com.example.sell.bean.ProductCategory;
import com.example.sell.bean.ProductInfo;
import com.example.sell.common.ResultVOUtil;
import com.example.sell.enums.ResultVO;
import com.example.sell.interlayer.ProductCategoryService;
import com.example.sell.service.CategoryService;
import com.example.sell.service.ProductService;
import com.example.sell.vo.ProductInfoVO;
import com.example.sell.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/7/1
 */
@RequestMapping("/category/")
@RestController
public class ProductCategoryContorller {
    @Autowired
    protected ProductCategoryService productCategoryService;


    /**
     * 查询所有商品类目下商品信息
     *
     * @return
     */

    @GetMapping("list")
    public ResultVOUtil list() {
        return ResultVOUtil.createBySuccessMessage("成功", productCategoryService.list());
    }

    @GetMapping("find_one/{categoryId}")
    public ResultVOUtil<Optional<ProductCategory>> findOne(@PathVariable("categoryId") Integer categoryId) {
        ResultVOUtil<Optional<ProductCategory>> productCategory = productCategoryService.findOne(categoryId);
        return productCategory;
    }

    /**
     * 添加商品类目信息
     *
     * @param productCategory
     * @param bindingResult
     * @return
     */
    @PostMapping("add_category")
    public ResultVOUtil<ProductCategory> addCategory(@Valid ProductCategory productCategory, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.createByErroCodeMessage(ResultVO.ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVOUtil<ProductCategory> addCategory = productCategoryService.save(productCategory);
        return addCategory;
    }
}
